package Test;

import java.util.ArrayList;
import java.util.Date;

import junit.framework.TestCase;

import org.junit.Test;

import business.Accounts;
import data.LMA;
import data.Scheduler;
import data.Volunteer;

/**
 * This is JUnit test for Account class
 * 
 * @author Minh Thai, Team 5
 *
 */
public class AccountsTest extends TestCase {


	/**
	 * Default test for constructor, this check that the arrayList is not null after instantiation
	 */
	@Test
	public void testAccounts() {
		Accounts t = new Accounts();
		assertNotNull(t);
		assertNotNull(t);
		assertEquals(t.getVolunteerList().isEmpty(), true);
		assertEquals(t.getLmaList().isEmpty(), true);
		assertEquals(t.getTheScheduler().isEmpty(), true);
		t = null;
	}

	/**
	 * Test AddAccount method
	 * 
	 * 1) add Volunteer account that already register 
	 * 2) add Scheduler account that already register
	 * 3) add LMA account that already register
	 * 4) add Volunteer account that already register, but username already exist within volunteer
	 *    collection
	 * 5) add LMA account that have register with blank name
	 */
	@Test
	public void testAddAccount() {
		Accounts acc = new Accounts();
		//1)
		Volunteer t = new Volunteer("", "", "a", "", "", "", "", false);
		acc.addAccount(t);
		assertTrue(acc.getVolunteerList().contains(t));
		//2)
		Scheduler s = new Scheduler("a", "", "", ""); 
		acc.addAccount(s);
		assertTrue(acc.getTheScheduler().contains(s));
		//3)
		LMA l = new LMA("", "a", "", "", ""); 
		acc.addAccount(l);
		assertTrue(acc.getLmaList().contains(l));
		//4)
		@SuppressWarnings("deprecation")
		Volunteer t2 = new Volunteer("", "", "a", "", "", "", (new Date(10,10,2010)).toString(), false);
		acc.addAccount(t2);
		int count = 0;
		for(Volunteer x : acc.getVolunteerList()) {
			if (x.equals(t2)) {
				count++;
			}
		}
		assertTrue(count == 1);		
		//5)
		LMA l2 = new LMA("", "", "", "", ""); 
		acc.addAccount(l2);
		assertFalse(acc.getLmaList().contains(l2));
		acc = null;	
	}

	/**
	 * Test getAccount() method
	 * 
	 * 1) test null input
	 * 2) test blank space input
	 * 3) test regular input, but not exist in database
	 * 4) test regular input for LMA, exist in database
	 * 5) test regular input for Scheduler, exist in database
	 * 6) test regular input for Volunteer, exist in database
	 */
	@Test
	public void testGetAccount() {
		Accounts acc = new Accounts();
		//1)
		assertNull(acc.getAccount(null));
		//2)
		assertNull(acc.getAccount("  "));
		//3)
		assertNull(acc.getAccount("Sam"));
		//4)
		LMA l = new LMA("", "username", "", "", ""); 
		acc.addAccount(l);
		assertNotNull(acc.getAccount(l.getUsername()));
		//5)
		Scheduler s = new Scheduler("username", "", "", "");
		acc.addAccount(s);
		assertNotNull(acc.getAccount(s.getUsername()));
		//6)
		@SuppressWarnings("deprecation")
		Volunteer v = new Volunteer("", "", "username", "", "", "", (new Date(10,10,10)).toString(), false);
		acc.addAccount(v);
		assertNotNull(acc.getAccount(v.getUsername()));
		acc = null;	
	}

	/**
	 * Test deleteAccount() method
	 * 
	 * 1) test remove account that does not exist
	 * 2) test remove volunteer account that exist
	 * 3) test remove scheduler account that exist
	 * 4) test remove lma account that exist
	 * 5) test with null parameter
	 */
	@Test
	public void testDeleteAccount() {
		Accounts acc = new Accounts();
		//1)
		ArrayList<Volunteer> vl = acc.getVolunteerList();
		ArrayList<Scheduler> sl = acc.getTheScheduler();
		ArrayList<LMA> ll = acc.getLmaList();
		acc.deleteAccount("123");
		assertTrue(acc.getVolunteerList().equals(vl) &&
					acc.getTheScheduler().equals(sl) &&
					acc.getLmaList().equals(ll));
		//2)
		@SuppressWarnings("deprecation")
		Volunteer v = new Volunteer("", "", "v", "", "", "",
					(new Date(10,10,10)).toString(), false);
		acc.addAccount(v);
		acc.deleteAccount("v");
		assertNull(acc.getAccount(v.getUsername()));
		//3)
		Scheduler s = new Scheduler("s", "", "", ""); 
		acc.addAccount(s);
		acc.deleteAccount("s");
		assertNull(acc.getAccount(s.getUsername()));
		//4)
		LMA l = new LMA("", "", "", "", ""); 
		acc.addAccount(l);
		//5)
		vl = acc.getVolunteerList();
		sl = acc.getTheScheduler();
		ll = acc.getLmaList();
		acc.deleteAccount(null);	
		assertTrue(acc.getVolunteerList().equals(vl) &&
					acc.getTheScheduler().equals(sl) &&
					acc.getLmaList().equals(ll));
	}

}
